import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './assets/css/common.less'
import axios from 'axios'

axios.defaults.baseURL = ''
axios.interceptors.request.use(config => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})
Vue.prototype.$http = axios
Vue.config.productionTip = false

new Vue({
  router,
  methods: {
    dateFormat(date, fmt) {
      fmt = fmt || 'yyyy-MM-dd HH:mm:ss'
      var o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'H+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        S: date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          )
        }
      }
      return fmt
    },
    browserVersion: function() {
      var userAgent = navigator.userAgent // 取得浏览器的userAgent字符串

      if (
        userAgent.match(
          /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        )
      ) {
        var isIos = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // 判断移动端浏览器
        var isAndroid =
          userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1 // 判断移动端浏览器
        if (isIos) {
          return 'ios'
        }
        if (isAndroid) {
          if (userAgent.match(/HUAWEI/i) && userAgent.match(/MQQBrowser/i)) {
            return 'huawei'
          } else if (
            userAgent.match(/HONOR/i) &&
            userAgent.match(/MQQBrowser/i)
          ) {
            return 'honor'
          } else {
            return 'android'
          }
        }
      } else {
        var isOpera = userAgent.indexOf('Opera') > -1 // 判断是否Opera浏览器
        var isIE =
          userAgent.indexOf('compatible') > -1 &&
          userAgent.indexOf('MSIE') > -1 &&
          !isOpera // 判断是否IE浏览器
        var isIE11 = userAgent.indexOf('rv:11') > -1
        var isEdge = userAgent.indexOf('Edge') > -1 && !isIE // 判断是否IE的Edge浏览器
        var isFF = userAgent.indexOf('Firefox') > -1 // 判断是否Firefox浏览器
        var isSafari =
          userAgent.indexOf('Safari') > -1 && userAgent.indexOf('Chrome') === -1 // 判断是否Safari浏览器
        var isChrome =
          userAgent.indexOf('Chrome') > -1 &&
          userAgent.indexOf('Safari') > -1 &&
          userAgent.indexOf('Edge') === -1 // 判断Chrome浏览器

        if (isIE) {
          var reIE = new RegExp('MSIE (\\d+\\.\\d+);')
          reIE.test(userAgent)
          var fIEVersion = parseFloat(RegExp.$1)
          switch (fIEVersion) {
            case 6:
              return '6'
            case 7:
              return '7'
            case 8:
              return '8'
            case 9:
              return '9'
            case 10:
              return '10'
            case 11:
              return '11'
            default:
              return '0' // IE版本过低
          }
        }
        if (isFF) {
          return 'FF'
        }
        if (isOpera) {
          return 'Opera'
        }
        if (isSafari) {
          return 'Safari'
        }
        if (isChrome) {
          return 'Chrome'
        }
        if (isEdge) {
          return 'Edge'
        }
        if (isIE11) {
          return '11'
        }
      }
    },
    replaceImg: function() {
      function init() {
        const imgs = document.querySelectorAll('.J-replace-img')
        if (imgs.length > 0) {
          var winW = window.innerWidth
          for (let i = 0; i < imgs.length; i++) {
            var $this = imgs[i]
            var src = $this.getAttribute('src')
            var img1920 = $this.getAttribute('data-pc-l')
            var img1600 = $this.getAttribute('data-pc-s')
            var img1200 = $this.getAttribute('data-ipad-h')
            var img992 = $this.getAttribute('data-ipad-v')
            var img750 = $this.getAttribute('data-wap')
            if (winW > 1600) {
              $this.setAttribute(
                'src',
                img1920 || img1600 || img1200 || img992 || img750 || src
              )
            } else if (winW > 1200) {
              $this.setAttribute(
                'src',
                img1600 || img1920 || img1200 || img992 || img750 || src
              )
            } else if (winW > 992) {
              $this.setAttribute(
                'src',
                img1200 || img1600 || img1920 || img992 || img750 || src
              )
            } else if (winW > 750) {
              $this.setAttribute(
                'src',
                img992 || img1200 || img1600 || img1920 || img750 || src
              )
            } else {
              $this.setAttribute(
                'src',
                img750 || img992 || img1200 || img1600 || img1920 || src
              )
            }
          }
        }
      }
      // document.querySelector('body').addEventListener('replaceImg', init)
      init()
    }
  },
  mounted() {},
  render: h => h(App)
}).$mount('#app')
